package itbour.onetouchshow.activity.video.videopreview;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;
import com.xiao.nicevideoplayer.NiceVideoPlayer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.version1_6.MakeActivityV1_6;
import itbour.onetouchshow.base.BaseIJKPlayActivity;
import itbour.onetouchshow.bean.LoadVideoUserDocBg;
import itbour.onetouchshow.bean.VideoBean;
import itbour.onetouchshow.bean.VideoPreviewBean;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.platform.WorkPlatform;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com44\\
 * <p>
 * 个人中心跳转到视频预览
 */

@Deprecated
public class VideoProudctPreviewActivity extends BaseIJKPlayActivity<VideoPreviewContract.View, VideoPreviewPresenter> implements VideoPreviewContract.View {

    @BindView(R.id.fragment_content)
    FrameLayout fragmentContent;
    @BindView(R.id.id_root_ry)
    RelativeLayout id_root_ry;
    /*视频数据*/
    VideoBean videoBean;
    /*作品预览*/
    //VideoPreviewFragment videoPreviewFragment;
    /*作品核心数据*/
    VideoPreviewBean videoPreviewBean;
    /*背景数据*/
    VideoPreviewBean.BgInfoBean bgInfo;
    /*背景列表*/
    List<VideoPreviewBean.BgInfoBean.ListBean> mbgData;

    //BgSelectorWindow bgSelectorWindow;

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {
        dismissProgressDialog();
    }


    @Override
    public int getFragmentContentId() {
        return R.id.fragment_content;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("作品预览");
        getToolBarView().setRightText("修改作品").getRightTextView().setVisibility(View.VISIBLE);
        getToolBarView().getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoPreviewBean.getWatermark() == 1) {
                    getDocString(videoPreviewBean.getId() + "");
                } else {
                    //如果已经去水印  重新拷贝生成
                    getCopyDocStringAndNew(videoPreviewBean.getId() + "");
                }
                // openActivity(MainActivity.class);
                //finish();
            }
        });
        getToolBarView().getLeftimageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBack();
            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_videopro_preview;
    }


    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        Bundle extras = getIntent().getExtras();
        String docId = null;
        if (extras != null) {
            docId = extras.getString(AppConst.DOCID);
        }
        if (TextUtils.isEmpty(docId)) {
            T_.showToastReal("没有作品ID");
            return;
        }
        mPresenter.loadDocPreview(docId);
        showProgressDialogWithText("正在获取预览");
    }

    @Override
    protected NiceVideoPlayer getmNiceVideoPlayer() {
        return null;
    }

    /**
     * <p>
     * 在获取作品预览
     * <p>
     * 设置背景后重新获取预览数据
     */
    @Override
    public void loadDocPreviewSuccess(String result) {
        L_.e(result);
        dismissProgressDialog();
        videoPreviewBean = new Gson().fromJson(result, VideoPreviewBean.class);
        bgInfo = videoPreviewBean.getBgInfo();
        mbgData = bgInfo.getList();
        videoBean = videoPreviewBean.getVideo();

        // videoPreviewFragment = new VideoPreviewFragment(videoPreviewBean, WorkPlatform.WOEK);
        //  fragmentContent.setVisibility(View.VISIBLE);
        // addFragment(videoPreviewFragment);
    }

    @Override
    public void loadNewDocPreviewSuccess(String result) {

    }


    /*去水印完成*/
    @Override
    public void removeWaterLoadDocPreview(String result) {

    }

    @Override
    public void createWaterOrderSuccess(String result) {

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void showFromStaus(String result) {
        videoPreviewBean = new Gson().fromJson(result, VideoPreviewBean.class);
    }

    @Override
    public void deleteVideoBgSuccess(String result) {

    }

    // ComplexDialogFrag  complexDialogFrag;
    @SuppressLint("ValidFragment")
    @Override
    public void downLoadVideoSuccess(String string) {
        dismissProgressDialog();
//        DialogUtils.getInstance(new DialogUtils.Builder().setTitle(" ")
//                .setConfirmText("确定").setConfirmListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        DialogUtils.getInstance().dismiss();
//                    }
//                }).setContext("已成功下载到本地").setPlatform(DialogUtils.DialogPlatform.ONE_BTN))
//                .show(getFragmentManager().beginTransaction(), "dialogUtils");

//        complexDialogFrag = new ComplexDialogFrag() {
//            @Override
//            public Builder setBuilder() {
//                return new Builder().setTitle(" ")
//                        .setConfirmText("确定").setConfirmListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                complexDialogFrag.dismiss();
//                            }
//                        }).setContext("已成功下载到本地").setPlatform(DialogPlatform.ONE_BTN);
//            }
//        };
//        complexDialogFrag.show(getFragmentManager().beginTransaction(), null);

    }

    @Override
    public void copyAndNewProductSuccess(String result) {

    }

    @Override
    public void getDocStringPreview(String result) {

    }

    @Override
    public void getDownloadUrl(String result) {

    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //removeFragment();
            clickBack();
            return true;
        }
        return false;
    }

    /**
     * 返回事件
     */
    private void clickBack() {
        finish();
    }

    /**
     * threadMode 执行的线程方式
     * priority 执行的优先级
     * sticky 粘性事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 50, sticky = true)
    public void paySuccess(Integer code) {
        L_.e("支付成功 去水印");
        // 如果有一个地方用 EventBus 发送一个 String 对象，那么这个方法就会被执行
        if (code == 1000) {
            showProgressDialogWithText("正在努力去水印");
            //videoPreviewFragment.geToRemoveWater.setText("去水印(以付款)");
            mPresenter.removeWatermark(videoPreviewBean.getId() + "");
        }
        if (code == 1200) {
            //视频背景修改后
            if (fragmentContent.getVisibility() == View.VISIBLE) {
                // videoPreviewFragment.mPresenter.loadDocPreview(videoPreviewBean.getId() + "");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onPause() {
        dismissProgressDialog();
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
              /*获取作品预览 */
            mPresenter.showFromStaus(videoPreviewBean.getId() + "");
//            if (videoPreviewFragment != null) {
//                videoPreviewFragment.onActivityResult(requestCode, resultCode, data);
//            }
        }
    }


    @Override
    public void loadVideoUserDocBgSucceed(String result) {
        //L_.e("loadVideoUserDocBgSucceed" + result);
        LoadVideoUserDocBg loadVideoUserDocBg = new Gson().fromJson(result, LoadVideoUserDocBg.class);
        List<LoadVideoUserDocBg.ListBean> list = loadVideoUserDocBg.getList();

    }


    /*
    获取作品数据
    * */
    public void getDocString(String docId) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        //  L_.e(videoBean.getId() + "");
        stringStringHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringStringHashMap.put("id", docId);
        stringStringHashMap.put("userId", App.getUserId());
        stringStringHashMap.put("isTmpl", 0);
        HttpUtils.getInstance().executeGet(AppConfig.GET_DOC_CONTENT_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showToastReal(exception);
            }

            @Override
            protected void onSuccess(String response) {
                Bundle bundle = new Bundle();
                bundle.putString("module", response);
                bundle.putString(WorkPlatform.TYPE, WorkPlatform.W0RK);
                openActivity(MakeActivityV1_6.class, bundle);
            }
        });

    }


    /*
  获取作品数据
  * */
    public void getCopyDocStringAndNew(String docId) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        //  L_.e(videoBean.getId() + "");
        stringStringHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringStringHashMap.put("docId", docId);
        stringStringHashMap.put("userId", App.getUserId());
        HttpUtils.getInstance().executeGet(AppConfig.GETUSERDOCCOPYTMPL_V1_0, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showToastReal(exception);
            }

            @Override
            protected void onSuccess(String response) {
                Bundle bundle = new Bundle();
                bundle.putString("module", response);
                bundle.putString(WorkPlatform.TYPE, WorkPlatform.MOUDLE);
                openActivity(MakeActivityV1_6.class, bundle);

            }
        });

    }

    @Override
    protected int videoHeight() {
        return UIUtils.getHForScan(videoBean.getW(), videoBean.getH());
    }


}
